package org.fh.inductionLamp.impl.response;

import org.fh.HandleData;
import org.fh.common.ResponseCode;
import org.fh.domain.DataFrame;
import org.fh.domain.TcpData;
import org.fh.inductionLamp.api.LampResponse;
import org.fh.inductionLamp.impl.request.GetConfigRequest;
import org.fh.utils.ConvertCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * @Author: wwy
 * @Date: 2020/10/22 10:08
 */
public class GetConfigResponse implements LampResponse {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetConfigResponse.class);
    private boolean success;
    private String code;
    private String data;
    private String deviceId;//设备id
    private String deviceType;//设备类型
    private String version;//控制器版本号
    private String controllerName;//控制器名称
    private String department;//所属部门
    private String controllerInstallAddress;//控制器安装位置
    private String localIp;//设备本地ip
    private String subnetMask;//子网掩码
    private String gateway;//网关
    private String tcpControllerPort;//tcp服务端口
    private String udpControllerPort;//udp服务端口
    private String serviceIp1;//服务器1ip地址
    private String servicePort1;//服务器1端口
    private String serviceIp2;//服务器2ip地址
    private String servicePort2;//服务器2端口
    private String channel;//传输通道
    private String isSendControlSignal;//是否发送控制信号标志
    private String controllerStartId;//控制器控制起始id
    private String controllerStartIdScope1;//控制器范围1起始id
    private String controllerEndIdScope1;//控制器范围1结束id
    private String controllerStartIdScope2;//控制器范围2起始id
    private String controllerEndIdScope2;//控制器范围2结束id




    /**
     * 无参构造
     * 用来循环获取返回的信息，直到数据获取
     * 等待时间默认3秒
     */
    public GetConfigResponse(String destId){
        this.deviceId = destId;
        HashMap<String, DataFrame> tcpDataHashMap = HandleData.tcpDataHashMap;
        int count = 0;

        while (true){
            if(count == 30){
                this.code = ResponseCode.TIME_OUT;
                this.success = false;
                break;
            }
            DataFrame dataFrame = tcpDataHashMap.get(destId);
            if(dataFrame != null){
                if(dataFrame.getDataStr() != null && !"".equals(dataFrame.getDataStr())){
                    this.code = ResponseCode.SUCCESS;
                    this.success = true;
                    this.data = dataFrame.getDataStr();
                    this.dataHandle(data);
                    //删除该条数据
                    tcpDataHashMap.remove(destId);
                    break;
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                LOGGER.info("线程异常{}",e.getMessage());
            }
            count ++;
        }

    }

    /**
     * 数据处理
     * @param data
     */
    public void dataHandle(String data){
        String one = data.substring(0, 4);
        String one2b = ConvertCode.hexString2binaryString(one);//二进制字符

        this.deviceType = Integer.parseInt(one2b.substring(0,6),2)+"";
        this.version = Integer.parseInt(one2b.substring(6,one2b.length()),2)+"";
        String two = data.substring(16, 16 + 64);
        String towStr = ConvertCode.hexStr2Str(two);
        this.controllerName = towStr;
        String three = data.substring(80, 80 + 24);
        String threeStr = ConvertCode.hexStr2Str(three);
        this.department = threeStr;
        String four = data.substring(104, 104 + 80);
        String fourStr = ConvertCode.hexStr2Str(four);
        this.controllerInstallAddress = fourStr;
        String five = data.substring(184, 184 + 8);
        this.localIp = Integer.parseInt(five.substring(0,2),16)
                +"."+Integer.parseInt(five.substring(2,4),16)
                +"."+Integer.parseInt(five.substring(4,6),16)
                +"."+Integer.parseInt(five.substring(6,8),16);

        String six = data.substring(192, 192+8);
        this.subnetMask = Integer.parseInt(six.substring(0,2),16)
                +"."+Integer.parseInt(six.substring(2,4),16)
                +"."+Integer.parseInt(six.substring(4,6),16)
                +"."+Integer.parseInt(six.substring(6,8),16);
        String seven = data.substring(200, 200+8);
        this.gateway = Integer.parseInt(seven.substring(0,2),16)
                +"."+Integer.parseInt(seven.substring(2,4),16)
                +"."+Integer.parseInt(seven.substring(4,6),16)
                +"."+Integer.parseInt(seven.substring(6,8),16);
        String eight = data.substring(208, 208+4);
        this.tcpControllerPort = Integer.parseInt(eight.substring(2,4)+eight.substring(0,2),16)+"";
        String nine = data.substring(212, 212+4);
        this.udpControllerPort = Integer.parseInt(nine.substring(2,4)+nine.substring(0,2),16)+"";
        String ten = data.substring(216,216+8);
        this.serviceIp1 = Integer.parseInt(ten.substring(0,2),16)
                +"."+Integer.parseInt(ten.substring(2,4),16)
                +"."+Integer.parseInt(ten.substring(4,6),16)
                +"."+Integer.parseInt(ten.substring(6,8),16);
        String eleven = data.substring(224,224+4);
        this.servicePort1 = Integer.parseInt(eleven.substring(2,4)+eleven.substring(0,2),16)+"";
        String twelve = data.substring(228,228+8);
        this.serviceIp2 = Integer.parseInt(twelve.substring(0,2),16)
                +"."+Integer.parseInt(twelve.substring(2,4),16)
                +"."+Integer.parseInt(twelve.substring(4,6),16)
                +"."+Integer.parseInt(twelve.substring(6,8),16);
        String thirteen = data.substring(236,236+4);
        this.servicePort2 = Integer.parseInt(thirteen.substring(2,4)+thirteen.substring(0,2),16)+"";
        String fourteen = data.substring(240,240+2);
        this.channel = Integer.parseInt(fourteen,16)+"";
        String fifteen = data.substring(242, 242+2);
        this.isSendControlSignal = Integer.parseInt(fifteen,16)+"";
        String sixteen = data.substring(244, 244+4);
        this.controllerStartId = Integer.parseInt(sixteen.substring(2,4)+sixteen.substring(0,2),16)+"";
        String seventeen = data.substring(248, 248+4);
        this.controllerStartIdScope1 = Integer.parseInt(seventeen.substring(2,4)+seventeen.substring(0,2),16)+"";
        String eighteen = data.substring(252, 252+4);
        this.controllerEndIdScope1 = Integer.parseInt(eighteen.substring(2,4)+eighteen.substring(0,2),16)+"";
        String nineteen = data.substring(256, 256+4);
        this.controllerStartIdScope2 = Integer.parseInt(nineteen.substring(2,4)+nineteen.substring(0,2),16)+"";
        String twenty = data.substring(260, 260+4);
        this.controllerEndIdScope2 = Integer.parseInt(twenty.substring(2,4)+twenty.substring(0,2),16)+"";
    }

    @Override
    public boolean isSuccess() {
        return this.success;
    }

    @Override
    public String code() {
        return this.code;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getControllerName() {
        return controllerName;
    }

    public void setControllerName(String controllerName) {
        this.controllerName = controllerName;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getControllerInstallAddress() {
        return controllerInstallAddress;
    }

    public void setControllerInstallAddress(String controllerInstallAddress) {
        this.controllerInstallAddress = controllerInstallAddress;
    }

    public String getLocalIp() {
        return localIp;
    }

    public void setLocalIp(String localIp) {
        this.localIp = localIp;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getSubnetMask() {
        return subnetMask;
    }

    public void setSubnetMask(String subnetMask) {
        this.subnetMask = subnetMask;
    }

    public String getGateway() {
        return gateway;
    }

    public void setGateway(String gateway) {
        this.gateway = gateway;
    }

    public String getTcpControllerPort() {
        return tcpControllerPort;
    }

    public void setTcpControllerPort(String tcpControllerPort) {
        this.tcpControllerPort = tcpControllerPort;
    }

    public String getUdpControllerPort() {
        return udpControllerPort;
    }

    public void setUdpControllerPort(String udpControllerPort) {
        this.udpControllerPort = udpControllerPort;
    }

    public String getServiceIp1() {
        return serviceIp1;
    }

    public void setServiceIp1(String serviceIp1) {
        this.serviceIp1 = serviceIp1;
    }

    public String getServicePort1() {
        return servicePort1;
    }

    public void setServicePort1(String servicePort1) {
        this.servicePort1 = servicePort1;
    }

    public String getServiceIp2() {
        return serviceIp2;
    }

    public void setServiceIp2(String serviceIp2) {
        this.serviceIp2 = serviceIp2;
    }

    public String getServicePort2() {
        return servicePort2;
    }

    public void setServicePort2(String servicePort2) {
        this.servicePort2 = servicePort2;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getIsSendControlSignal() {
        return isSendControlSignal;
    }

    public void setIsSendControlSignal(String isSendControlSignal) {
        this.isSendControlSignal = isSendControlSignal;
    }

    public String getControllerStartId() {
        return controllerStartId;
    }

    public void setControllerStartId(String controllerStartId) {
        this.controllerStartId = controllerStartId;
    }

    public String getControllerStartIdScope1() {
        return controllerStartIdScope1;
    }

    public void setControllerStartIdScope1(String controllerStartIdScope1) {
        this.controllerStartIdScope1 = controllerStartIdScope1;
    }

    public String getControllerEndIdScope1() {
        return controllerEndIdScope1;
    }

    public void setControllerEndIdScope1(String controllerEndIdScope1) {
        this.controllerEndIdScope1 = controllerEndIdScope1;
    }

    public String getControllerStartIdScope2() {
        return controllerStartIdScope2;
    }

    public void setControllerStartIdScope2(String controllerStartIdScope2) {
        this.controllerStartIdScope2 = controllerStartIdScope2;
    }

    public String getControllerEndIdScope2() {
        return controllerEndIdScope2;
    }

    public void setControllerEndIdScope2(String controllerEndIdScope2) {
        this.controllerEndIdScope2 = controllerEndIdScope2;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    @Override
    public String toString() {
        return "GetConfigResponse{" +
                "success=" + success +
                ", code='" + code + '\'' +
                ", data='" + data + '\'' +
                ", deviceId='" + deviceId + '\'' +
                ", deviceType='" + deviceType + '\'' +
                ", version='" + version + '\'' +
                ", controllerName='" + controllerName + '\'' +
                ", department='" + department + '\'' +
                ", controllerInstallAddress='" + controllerInstallAddress + '\'' +
                ", localIp='" + localIp + '\'' +
                ", subnetMask='" + subnetMask + '\'' +
                ", gateway='" + gateway + '\'' +
                ", tcpControllerPort='" + tcpControllerPort + '\'' +
                ", udpControllerPort='" + udpControllerPort + '\'' +
                ", serviceIp1='" + serviceIp1 + '\'' +
                ", servicePort1='" + servicePort1 + '\'' +
                ", serviceIp2='" + serviceIp2 + '\'' +
                ", servicePort2='" + servicePort2 + '\'' +
                ", channel='" + channel + '\'' +
                ", isSendControlSignal='" + isSendControlSignal + '\'' +
                ", controllerStartId='" + controllerStartId + '\'' +
                ", controllerStartIdScope1='" + controllerStartIdScope1 + '\'' +
                ", controllerEndIdScope1='" + controllerEndIdScope1 + '\'' +
                ", controllerStartIdScope2='" + controllerStartIdScope2 + '\'' +
                ", controllerEndIdScope2='" + controllerEndIdScope2 + '\'' +
                '}';
    }
}
